<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
</head>
<body>
    <input type="button" value="我是添加按钮" id="btn">
    <div id="mydiv"></div>
    <script>
        var btn=document.getElementById("btn");
        var mydiv=document.getElementById("mydiv");
        btn.onclick=function(){
           /*  for(var i=0;i<=1;i++){
                var p=document.createElement("p");
                p.innerHTML="我是p标签"+i;
                p.onclick=()=>{
                    console.log(this);
                };
                mydiv.appendChild(p);
            } */
            mydiv.innerHTML="<p>我是p标签1</p><p>我是p标签2</p>";
            var p=document.getElementsByTagName("p");
            for(var i=0;i<p.length;i++){
                p[i].onclick=()=>{
                    console.log(this);
                };
            }
           
        };
       
    </script>
</body>
</html>